/**
 * 
 */
package menus.zone;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Point2D;

import fr.lri.swingstates.canvas.CElement;
import fr.lri.swingstates.canvas.CPolyLine;
import fr.lri.swingstates.canvas.CSegment;
import fr.lri.swingstates.canvas.CShape;
import menus.common.AbstractItem;
import menus.common.AbstractMenu;
import menus.common.TapAndStrokeMenu;

/**
 * @author insitu
 *
 */
public class ZoneMenuItem extends AbstractItem {
	
	protected double zoneAngle;
	protected double itemAngle;
	
	public static double arrowUnit = 10;
	
	/**
	 * @param menu
	 * @param label
	 */
	public ZoneMenuItem(
			TapAndStrokeMenu menu, 
			String label, 
			double zoneAngle, 
			double itemAngle,
			String tag
	) {
		
		super(menu, label, tag);
		
		this.zoneAngle = zoneAngle;
		this.itemAngle = itemAngle;
		
	}

	@Override
	public CShape createShape(){
		return createShape(((ZoneMenu)menu).getItemLength(), ((ZoneMenu)menu).getMenuRadius());
	}
	
	public CShape createShape(double itemLength, double menuRadius) {
		
		return createShape(zoneAngle, itemAngle, itemLength, menuRadius, tag);
		
	}
	
	public static CShape createShape(double zoneAngle, double itemAngle, double itemLength, double menuRadius, String tag) {
		
		CPolyLine mark = new CPolyLine(0, 0);
		
		mark.setOutlined(true)
			.setOutlinePaint(Color.blue)
			.setStroke(new BasicStroke(3))
			.setFilled(false)
			
			.addTag(tag)
			
			.setPickable(false)
			.setDrawable(false);
		
		mark.lineTo(itemLength, 0)
			.lineTo(itemLength - arrowUnit, arrowUnit)
			.lineTo(itemLength - arrowUnit, - arrowUnit)
			.lineTo(itemLength, 0)
			
			.setReferencePoint(.5, .5)
			.translateTo(menuRadius * Math.cos(zoneAngle), menuRadius * -Math.sin(zoneAngle))
			.rotateTo(-itemAngle);
		
		return mark;
		
	}
	
	protected CSegment[] createSeparators(){
		
		int nbSeparators = menu.getItemsLength();
		CSegment[] result = new CSegment[2];
		
		for (int i = 0; i < 2; i++){
			CSegment separator = new CSegment(0, 0, 2*((ZoneMenu)menu).getMenuRadius(), 0);
			separator.setFilled(false)
					.setOutlinePaint(Color.darkGray)
					.setStroke(new BasicStroke(1, 0, 0, 1, new float[]{5, 5}, 0))
			
					.setDrawable(true)
					.setPickable(false)
					
					.addTag(tag)
					
					.setReferencePoint(0, 0)
					.rotateBy(-zoneAngle + 2*Math.PI/nbSeparators*(i-.5));
			
			result[i] = separator;
		}
		
		return result;
		
	}
	
	@Override
	public void ghost(Point2D where) {
		
		CSegment[] separators = createSeparators();
		
		for (CSegment s : separators){
			super.ghost(where, s);
		}
		
		super.ghost(where);
		super.ghost(where, ((TapAndStrokeMenu)menu).createTap());
		
		((TapAndStrokeMenu)menu).getTap().setDrawable(false);
	}

}
